package com.falcon.service.impl;

import java.util.List;

import com.falcon.domain.dos.LogDO;
import com.falcon.domain.po.LogPO;
import com.falcon.domain.req.LogQuery;
import com.falcon.domain.req.LogRequest;
import com.falcon.mapper.ILogMapper;
import com.falcon.service.ILogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.bean.BeanUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swan.mybatis.condition.Condition;
import com.falcon.mvc.domain.PagerResponse;
import com.falcon.core.utils.BeanUtils;


@Slf4j
@Service
public class LogService implements ILogService {

    @Autowired
    private ILogMapper logMapper;

    @Override
    public Long save(LogRequest request){
        LogPO logPO = BeanUtil.copyProperties(request, LogPO.class);

        this.logMapper.insertNotNull(logPO);

        return logPO.getId();
    }

    @Override
    public Boolean update(Long id, LogRequest request){

        LogPO logPO = BeanUtil.copyProperties(request, LogPO.class);
        logPO.setId(id);

        return this.logMapper.updateNotNull(logPO);
    }

    @Override
    public Boolean deleteById(Long id) {

        return this.logMapper.deleteById(id);
    }

    @Override
    public LogDO queryById(Long id){

        LogPO logPO = this.logMapper.selectById(id);

        LogDO logDO = BeanUtil.copyProperties(logPO, LogDO.class);

        return logDO;
    }


    @Override
    public List<LogDO> queryList(LogQuery request){

        Condition condition = request.toCondition();

        List<LogPO> poList = this.logMapper.selectList(condition);

        List<LogDO> doList = BeanUtil.copyToList(poList, LogDO.class);

        return doList;
    }

    @Override
    public PagerResponse<LogDO> queryPager(LogQuery request){


        PageInfo<LogPO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.logMapper.selectList(request.toCondition()));

        return BeanUtils.copyToPager(pageInfo, LogDO.class);
    }


}

